package dp;

import java.util.Arrays;

public class LCSubstring {
    //最长公共子序列
    public static void main(String[] args) {
        lcs("itheima", "thema");
    }

    private static void lcs(String m, String n) {
        int[][] dp = new int[n.length()][m.length()];
        int max = 0;
        for (int i = 0; i < n.length(); i++) {
            for (int j = 0; j < m.length(); j++) {
                if (m.charAt(j) == n.charAt(i)) {
                    if (i == 0 || j == 0) {
                        dp[i][j] = 1;
                    } else {
                        dp[i][j] = dp[i - 1][j - 1] + 1;
                        max=Integer.max(max,dp[i][j]);
                    }
                } else {
                    dp[i][j] = 0;
                }
            }
        }


        print(dp);
        System.out.println(max);
    }

    private static void print(int[][] dp) {
        System.out.println("=====");
        for (int[] ints : dp) {
            System.out.println(Arrays.toString(ints));
        }
        System.out.println("=====");
    }
}
